import becker.robots.City;
import becker.robots.Direction;
import becker.robots.Robot;

/**
 * The trailbot extends the Robot class to create a new robot that follows a
 * trail of 'Things' left for them.
 *
 * @author Mohammad Abubakr
 * @since March 23, 2012
 *
 */
public class TrailBot extends Robot
{
    int numberOfThings = 0; // Stores the number of things present on the
    // current intersection.
    int timesToMove = 1; //The number of times the robot must move.

    /**
     * Creates a new TrailBot with the given parameters.
     *
     * @param aCity
     *            The city the TrailBot is in.
     * @param aStreet
     *            The starting street of the TrailBot.
     * @param anAvenue
     *            The starting avenue of the TrailBot.
     * @param aDirection
     *            The direction the TrailBot is facing.
     */
    public TrailBot (City aCity, int aStreet, int anAvenue, Direction aDirection)
    {
	super (aCity, aStreet, anAvenue, aDirection);
    }


    /**
     * Counts the number of things present on the current intersection. Stores
     * this in an integer value.
     */
    private void countThings ()
    {
	numberOfThings = 0;
	while (super.canPickThing ())
	{ // Count number of things by picking them
	    // all up one by one.
	    super.pickThing ();
	    numberOfThings++;
	}
	for (int i = 0 ; i < numberOfThings ; i++)
	{ // Drop all of the held
	    // things, leaves trail
	    // unchanged.
	    super.putThing ();
	}
    }


    /**
     * @return The number of things on the current intersection.
     */
    public int getNumberOfThings ()
    {
	return numberOfThings;
    }


    /**
     * Moves the TrailBot based on the number of things on the intersection.
     */

    public void trailMove ()
    {
	this.countThings ();
	timesToMove = 1; // Stores the number of times the
	// robot must move.
	if (numberOfThings == 1)
	{ // Face the correct direction.
	    this.turnRight ();
	}
	else if (numberOfThings == 2)
	{
	    super.turnLeft ();
	}
	else if (numberOfThings >= 3)
	{
	    timesToMove = numberOfThings - 2;
	}
	while (timesToMove > 0)
	{
	    this.navigateWall ();
	    if (timesToMove > 0)
	    {
		super.move ();
	    }
	    --timesToMove;
	}
    }


    /**
     * If the robot's path is blocked by a wall, it will navigate around the
     * wall.
     */
    private void navigateWall ()
    {
	int timesMoved = 0;
	if (!super.frontIsClear ())
	{
	    this.turnRight ();
	    do
	    {
		timesMoved++;
		super.move ();
		super.turnLeft ();
		if (!super.frontIsClear ())
		{
		    this.turnRight ();
		}
	    }
	    while (!super.frontIsClear ());
	    super.move ();
	    super.turnLeft ();
	    super.move ();
	    super.turnLeft ();
	    for (int i = 0 ; i <= timesMoved ; i++)
	    {
		super.move ();
	    }
	    this.turnRight ();
	    timesToMove--;
	}

    }


    /**
     * Turn the TrailBot right.
     */
    private void turnRight ()
    {
	for (int i = 0 ; i < 3 ; i++)
	{
	    super.turnLeft ();
	}
    }
}
